package com.test.springCloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.test.springCloud.mapper.SessionListMapper;
import com.test.springCloud.mapper.UserMapper;
import com.test.springCloud.service.SessionListService;
import com.test.springCloud.utils.JwtUtil;
import com.test.springCloud.utils.ResultUtils;
import com.test.springcloud.pojo.SessionListPojo;
import com.test.springcloud.pojo.UserPojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 会话的接口
 */
@CrossOrigin  //跨域
@RequestMapping("/lxf")
@Slf4j
@RestController
public class SessionController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private SessionListService seesionListService;

    /**
     * 已建立会话
     */
    @GetMapping("/sessionList/already")
    public ResultUtils sessionListAlready(HttpServletRequest request){

        //解析token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        Integer id = Integer.parseInt(userId);

        List<SessionListPojo> sessionLists = seesionListService.selectByUserId(id);
        return  ResultUtils.getSuccess("success",sessionLists);
    }

    /**
     * 可建立会话的用户
     *
      */
    @GetMapping("/sessionList/not")
    public ResultUtils sessionListNot(HttpServletRequest request){

        //解析token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        Integer id = Integer.parseInt(userId);

        List<Integer> list = seesionListService.selectUserIdByUserId(id);
        list.add(id);
        List<UserPojo> cloudList = userMapper.getCloudList(list);
        return ResultUtils.getSuccess("success",cloudList);
    }

    /**
     *
     * 创建会话
      */
    @PostMapping("/createSession")
    public ResultUtils createSession(@RequestBody JSONObject req,HttpServletRequest request){

        //解析token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        Integer id = Integer.parseInt(userId);//当前用户id

        Integer toUserId = req.getInteger("toUserId");//发送到某个用户的用户id
        String toUserName = req.getString("toUserName");
        SessionListPojo sessionList = new SessionListPojo();
        sessionList.setUserId(id);
        sessionList.setUnReadCount(0);
        sessionList.setListName(toUserName);
        sessionList.setToUserId(toUserId);
        seesionListService.insert(sessionList);
        // 判断对方和我建立会话没有？ 没有也要建立
        Integer integer = seesionListService.selectIdByUser(toUserId, id);
        if (integer == null || integer <= 0){
            UserPojo user = userMapper.selectByPrimaryKey(id);
            sessionList.setUserId(toUserId);
            sessionList.setToUserId(id);
            sessionList.setListName(user.getUserName());
            seesionListService.insert(sessionList);
        }
        return ResultUtils.getSuccess("success");
    }

    /**
     *   删除会话
      */

    @GetMapping("/delSession")
    public ResultUtils delSession(@RequestBody JSONObject req){
        Integer sessionId = req.getInteger("sessionId");
        seesionListService.deleteByPrimaryKey(sessionId);
        return ResultUtils.getSuccess("success");
    }

}
